<?php

/**
 * @name: HOTN AnimeList
 * @author: HammerOnTheNail
 * @link: http://code.google.com/p/hotnanimelist/
 */

// Alap dolgok bekérése
include("../../class2.php");

// Ellenőrzés
if (e_QUERY)
{
	$id = (int)e_QUERY;
}
else
{
	header("Location: project_list.php");
}

header("Location: project_view.php?".$id."#download");

include(e_PLUGIN."/hotn_animelist/Languages/".e_LANGUAGE.".php");
include(e_PLUGIN."hotn_animelist/Class/animelist_mysql.php");
include(e_PLUGIN."hotn_animelist/Class/animelist_options.php");

//beállítások ellenőrzése:
if ($HOTN_ANIMELIST_OPTIONS['forum_type'] <> "" || $HOTN_ANIMELIST_OPTIONS['download_type']<>"")
{
	include(e_PLUGIN."hotn_animelist/Class/forum_".$HOTN_ANIMELIST_OPTIONS['forum_type'].".php");
	$HOTN_ANIMELIST_FORUM = new hotn_animelist_forum();

	include(e_PLUGIN."hotn_animelist/Class/download_".$HOTN_ANIMELIST_OPTIONS['download_type'].".php");
	$HOTN_ANIMELIST_DOWNLOAD = new hotn_animelist_download();
}

// hotn_animelist css file beolvasása
if (file_exists(THEME."hotn_animelist/animelist.css")) $eplug_css[] = THEME."hotn_animelist/animelist.css";
else $eplug_css[] = e_PLUGIN_ABS."hotn_animelist/Class/animelist.css";

// Javascript
$footer_js[] = e_PLUGIN_ABS."hotn_animelist/Class/js/jquery.js";
$footer_js[] = e_PLUGIN_ABS."hotn_animelist/Class/js/script.js";

// More icon
if (file_exists(THEME."hotn_animelist/more.png")) define("HOTN_ANIMELIST_DOWNLOAD_MORE",THEME."hotn_animelist/more.png");
else define("HOTN_ANIMELIST_DOWNLOAD_MORE",e_PLUGIN."hotn_animelist/Images/more.png");

// Project adatok lekérdezése
$project = $HOTN_ANIMELIST_DOWNLOAD->hotn_animelist_download_get_one_project($id);

// Get download
$project_type = $project['project_type'];
if ($project['project_type'] == "anime")
{
	$download["subtitle"] = $HOTN_ANIMELIST_DOWNLOAD->hotn_animelist_download_get_download($project['project_id'],'subtitle');
	$download["video"] = $HOTN_ANIMELIST_DOWNLOAD->hotn_animelist_download_get_download($project['project_id'],'anime');
}
else
{
	$download["manga"] = $HOTN_ANIMELIST_DOWNLOAD->hotn_animelist_download_get_download($project['project_id'],'manga');
}

$download_title = array(
	"subtitle" => HOTN_ANIMELIST_PROJECT_DOWNLOAD_SUBTITLE,
	"video" => HOTN_ANIMELIST_PROJECT_DOWNLOAD_ANIME,
	"manga" => HOTN_ANIMELIST_PROJECT_DOWNLOAD_MANGA
);

//print "<pre>";
//print_r($download);
//print "</pre>";

// Oldal címe
define("e_PAGETITLE",HOTN_ANIMELIST_PROJECT_DOWNLOAD_TITLE.$project['project_name']);
$title = HOTN_ANIMELIST_PROJECT_DOWNLOAD_TITLE.$project['project_name'];
// Dátum kiíró
$gen = new convert;

// Project állapota
if ($project['project_mode'] == "active") $themp = HOTN_ANIMELIST_ANIMETYPE_ACTIVE;
if ($project['project_mode'] == "finished") $themp = HOTN_ANIMELIST_ANIMETYPE_FINISHED;
if ($project['project_mode'] == "planned") $themp = HOTN_ANIMELIST_ANIMETYPE_PLANNED;
if ($project['project_mode'] == "passive") $themp = HOTN_ANIMELIST_ANIMETYPE_PASSIVE;
if ($project['project_mode'] == "dropped") $themp = HOTN_ANIMELIST_ANIMETYPE_DROPPED;
$project['project_mode'] = "<li class='hotn_animelist_project_download_mode'><span>".HOTN_ANIMELIST_PROJECT_VIEW_PROJECT_MODE."</span><a href='project.php?".$project['project_mode'].".all.10.0'>".$themp."</a></li>";

// Kezdés és befejezés dátuma
if ($project['project_date_end'] == 0) $project['project_date_end'] = HOTN_ANIMELIST_PROJECT_ACTIVE_PROJECT;
else $project['project_date_end'] = $gen->convert_date($project['project_date_start'],"long");
$project['project_date_end'] = "<li class='hotn_animelist_project_download_date_end'><span>".HOTN_ANIMELIST_PROJECT_PROJECT_DATE_END."</span>".$project['project_date_end']."</li>";
$project['project_date_start'] = "<li class='hotn_animelist_project_download_date_start'><span>".HOTN_ANIMELIST_PROJECT_PROJECT_DATE_START."</span>".$gen->convert_date($project['project_date_start'],"long")."</li>";


// Munkatársak
$sql->db_Select("hotn_animelist_team as team, ".MPREFIX."user as user","user.user_id, user.user_name","WHERE user.user_id = team.team_id ORDER BY user_name ASC","non-default");
while($row = $sql->db_Fetch())
{
	$HOTN_ANIMELIST_TEAM[$row['user_id']] = $row['user_name'];
}

$team_project_row = array(
	"project_translator" => HOTN_ANIMELIST_PROJECT_VIEW_PROJECT_TRANSLATOR,
	"project_karaoke" => HOTN_ANIMELIST_PROJECT_VIEW_PROJECT_KARAOKE,
	"project_format" => HOTN_ANIMELIST_PROJECT_VIEW_PROJECT_FORMAT,
	"project_manga_format" => HOTN_ANIMELIST_PROJECT_VIEW_PROJECT_MANGA_FORMAT,
	"project_video" => HOTN_ANIMELIST_PROJECT_VIEW_PROJECT_VIDEO,
	"project_lector" => HOTN_ANIMELIST_PROJECT_VIEW_PROJECT_LECTOR,
	"project_timer" => HOTN_ANIMELIST_PROJECT_VIEW_PROJECT_TIMER
);

foreach ($team_project_row as $id => $value)
{
	if ( ($project[$id] <> "") || ($project[$id."_other"]<>"") )
	{
		$user_links = array();
		foreach (explode("%",$project[$id]) as $user_explode)
		{
			$user_links[] = "<a href='project_team.php?".$user_explode."'>".$HOTN_ANIMELIST_TEAM[$user_explode]."</a>";
		}
		
		$user_links = implode(", ",$user_links);
		
		if ($project[$id."_other"]<>"")
		{
			if ($project[$id] <> "")
			{
				$user_links .= ", ";
			}
			$user_links .= $project[$id."_other"];
		}
		
		$project[$id] = "
		<li class='hotn_animelist_project_view_team'>
			<span>".$value."</span>
			".$user_links."
		</li>
		";
	}
}

// Anime típus kiírása
if ($project['project_type'] <> "manga")
{
	switch ($project['project_anime_type'])
	{
		case "tv": $project['project_anime_type'] = HOTN_ANIMELIST_ANIMETYPE_TV;
			break;
		case "ova": $project['project_anime_type'] = HOTN_ANIMELIST_ANIMETYPE_OVA;
			break;
		case "movie": $project['project_anime_type'] = HOTN_ANIMELIST_ANIMETYPE_MOVIE;
			break;
		case "ona": $project['project_anime_type'] = HOTN_ANIMELIST_ANIMETYPE_ONA;
			break;
		case "special": $project['project_anime_type'] = HOTN_ANIMELIST_ANIMETYPE_SPECIAL;
			break;
	}
	
	$project['project_anime_type'] = "<li class='hotn_animelist_project_download_anime_type'><span>".HOTN_ANIMELIST_PROJECT_VIEW_PROJECT_ANIME_TYPE."</span>".$project['project_anime_type']."</li>"; 
}
else
{
	// Ez egy manga, szóval nincs szükség típus kiírása
	$project['project_anime_type'] = "";
}

// Project típusa
if ($project['project_type'] == "anime") $themp = HOTN_ANIMELIST_DEFAULT_ANIME;
if ($project['project_type'] == "manga") $themp = HOTN_ANIMELIST_DEFAULT_MANGA;
$project['project_type'] = "<li class='hotn_animelist_project_download_type'><span>".HOTN_ANIMELIST_PROJECT_PROJECT_TYPE."</span><a href='project.php?all.".$project['project_type'].".10.0'>".$themp."</a></li>";

// Project neve
$project['project_name'] = "<li class='hotn_animelist_project_download_title'><span>".HOTN_ANIMELIST_PROJECT_VIEW_PROJECT_TITLE."</span>".$project['project_name']."</li>";

// Project ismertetője
if ($project['project_text'] <> "")
$project['project_text'] = "<li class='hotn_animelist_project_download_view'>[ <a href='".e_PLUGIN."hotn_animelist/project_view.php?".$project['project_id']."'>".HOTN_ANIMELIST_PROJECT_PROJECT_VIEW."</a> ]</li>";

// Fejléc
$text_project_content .= "
	<table class='hotn_animelist_project_download'>
		<tr>
			<td class='hotn_animelist_project_download_image'>
				<img src='".e_PLUGIN."hotn_animelist/Project/".$project['project_folder']."/".$project['project_image_reference']."' />
			</td>
			<td class='hotn_animelist_project_download_info'>
				<ul>
					".$project['project_name']."
					
					".$project['project_type']."
					".$project['project_anime_type']."

					".$project['project_translator']."
					".$project['project_karaoke']."
					".$project['project_format']."
					".$project['project_manga_format']."
					".$project['project_video']."
					".$project['project_lector']."
					".$project['project_timer']."
					
					".$project['project_date_start']."
					".$project['project_date_end']."
					".$project['project_text']."
				</ul>
			</td>
		</tr>
	</table>
";

foreach ($download as $download_category_name => $download)
{
	if (count($download) > 0)
	{
		$text_project_content .= "<table class='hotn_animelist_project_download_".$download_category_name."'>
			<tr>
				<td colspan='2' class='hotn_animelist_project_download_category_title'>
					".$download_title[$download_category_name]."
				</td>
			</tr>";
		// anime típusok kiírása
		if ($project_type == "anime")
		{
			$text_project_content .= "<tr class='hotn_animelist_project_download_head'>
				<td class='hotn_animelist_project_download_name_head'>
					".HOTN_ANIMELIST_PROJECT_DOWNLOAD_TITLE."
				</td>
				<td class='hotn_animelist_project_download_link_head'>
					".HOTN_ANIMELIST_PROJECT_DOWNLOAD_DOWNLOAD."
				</td>
			</tr>";
			
			$poz = 0;
			foreach ($download as $download)
			{
				$text_project_content .= showdownload($download,$poz++);
			}	
		}
		
		// manga típusok kiírása
		if ($project_type == "manga")
		{
			foreach ($download as $download_category)
			{
				$text_project_content .= "
					<tr>
						<td class='hotn_animelist_project_download_category_image'>
							<img src='".e_PLUGIN."hotn_animelist/Project/".$project['project_folder']."/".$download_category[0]['category_image']."' />
						</td>
						<td class='hotn_animelist_project_download_category_list'>
							<table>
								<tr class='hotn_animelist_project_download_head'>
									<td class='hotn_animelist_project_download_name_head'>
										".HOTN_ANIMELIST_PROJECT_DOWNLOAD_TITLE."
									</td>
									<td class='hotn_animelist_project_download_link_head'>
										".HOTN_ANIMELIST_PROJECT_DOWNLOAD_DOWNLOAD."
									</td>
								</tr>
				";
				$poz = 0;
				foreach ($download_category as $download_one)
				{
					$text_project_content .= showdownload($download_one,$poz++);
				}
				
				$text_project_content .= "
							</table>
						</td>
					</tr>
				";
			}
		}
		
		
		$text_project_content .= "</table>";
	}
}

// Oldal megjelenítése
require_once(HEADERF);
$ns -> tablerender($title, $text_project_content, "");
require_once(FOOTERF);

// author kiírása
function author($name,$id)
{
	if (is_numeric($id))
	{
		return "<a href='project_team.php?".$id."'>".$name."</a>";
	}
	else
	{
		return $name;
	}
	
}

// Letöltés kiírása
function showdownload($download,$poz)
{
	global $gen, $HOTN_ANIMELIST_DOWNLOAD;
	
	if ($poz%2 == 0) $poz_css = "hotn_animelist_project_download_list_highlite";
	
	return "
		<tr class='hotn_animelist_project_download_list ".$poz_css."' id='download_".$download['download_id']."'>
			<td class='hotn_animelist_project_download_name'>
				<a href='javascript:hotn_animelist_show(".$download['download_id'].")'>".$download['download_name']."</a> <a href='javascript:hotn_animelist_show(".$download['download_id'].")'><img src='".HOTN_ANIMELIST_DOWNLOAD_MORE."' /></a>
			</td>
			<td class='hotn_animelist_project_download_link'>
				".$HOTN_ANIMELIST_DOWNLOAD->hotn_animelist_download_link($download['download_id'])."
			</td>
		</tr>
		<tr class='hotn_animelist_project_download_list_more ".$poz_css."_more'>
			<td colspan='2' class='hotn_animelist_project_download_more' id='download_more_".$download['download_id']."'>
				<ul>
					<li class='hotn_animelist_project_download_more_filename'><span>".HOTN_ANIMELIST_PROJECT_DOWNLOAD_FILENAME."</span>".basename($download['download_url'])."</li>
					<li class='hotn_animelist_project_download_more_filename'><span>".HOTN_ANIMELIST_PROJECT_DOWNLOAD_FILESIZE."</span>".parsesize($download['download_filesize'])."</li>
					<li class='hotn_animelist_project_download_more_author'><span>".HOTN_ANIMELIST_PROJECT_DOWNLOAD_AUTHOR."</span>".author($download['download_author'],$download['user_id'])."</li>
					<li class='hotn_animelist_project_download_more_date'><span>".HOTN_ANIMELIST_PROJECT_DOWNLOAD_DATE."</span>".$gen->convert_date($download['download_datestamp'],"long")."</li>
					<li class='hotn_animelist_project_download_more_request'><span>".HOTN_ANIMELIST_PROJECT_DOWNLOAD_REQUEST."</span>".$download['download_request']."</li>
				</ul>
			</td>
		</tr>
	";
}

// az eredeti download.php-ból
function parsesize($size) {
	$kb = 1024;
	$mb = 1024 * $kb;
	$gb = 1024 * $mb;
	$tb = 1024 * $gb;
	if(!$size)
	{
		return '0';
	}
	if ($size < $kb) {
		return $size." ".CORE_LAN_B;
	}
	else if($size < $mb) {
		return round($size/$kb, 2)." ".CORE_LAN_KB;
	}
	else if($size < $gb) {
		return round($size/$mb, 2)." ".CORE_LAN_MB;
	}
	else if($size < $tb) {
		return round($size/$gb, 2)." ".CORE_LAN_GB;
	} else {
		return round($size/$tb, 2)." ".CORE_LAN_TB;
	}
}
?>